# code to analyze inferred conceptual networks estimated in pundits_shape_parrots.R
# results in Figure 1
library(tidyverse)
library(data.table)
library(dtplyr)
library(network)
library(igraph)
library(intergraph)
library(ggnetwork)

# ggplot theme
theme_jg <- function(){
    theme_classic()+
    theme(text = element_text(family = "serif", size = 16),
          strip.text = element_text(face = "bold"),
         plot.title = element_text(size = 20, face = "bold"))
}

# load data
load("../output/aggregate_nets/pundits_parrot_net_all.RData")

# reference mapping between informal/formal concept names
topic_refdf <- data.frame(formal_names = c("China","Class","Climate",
                                           "Conservative","Democrat",
                                           "Far Left","Far Right",
                                           "Gender","Guns","Health Care/Ins.",
                                           "Immigration","Iran","Israel","LGBT",
                                           "Liberal","Mueller","Progressive",
                                           "Race","Repro. Health","Republican",
                                           "Taxes and Spending",
                                           "Trade"),
                          topic = c('china','class','climate','conservative',
                                    'democrat',
                                    'far_left','far_right','gender','guns',
                                    'health_care_insurance','immigration',
                                    'iran','israel','lgbt','liberal',
                                    'mueller','progressive','race',
                                    'reproductive_health','republican',
                                    'taxes_spending','trade'),
                          group = c(1:22))

noderefs <- data.frame(node = as.character(sapply(topic_refdf$topic,
                                                  function(x){
                                                    paste0("X", 1:10, "_", x)
                                                  })),
                       topic = rep(topic_refdf$topic, each = 10),
                       dimension = rep(1:10, 22))


# extract graph from inferred adjacency matrix, shape
g <- igraph::graph_from_adjacency_matrix(pundits_net_withparty$sample$graph,
                                         weighted = TRUE,
                                         mode = "undirected")

graph_df <- as_long_data_frame(g)
names(graph_df) <- c('from','to','weight','from_name','to_name')

graph_df$from_topic <- sapply(graph_df$from_name, function(x){
  s <- strsplit(x, split = "_")[[1]]
  
  return(paste0(s[2:length(s)], collapse = "_"))
})

graph_df$to_topic <- sapply(graph_df$to_name, function(x){
  s <- strsplit(x, split = "_")[[1]]
  
  return(paste0(s[2:length(s)], collapse = "_"))
})

# flag when to/from concept are the same
graph_df$issue_match <- with(graph_df, as.numeric(from_topic == to_topic))

topic_agg_weights <- 
  graph_df %>%
  group_by(from_topic, to_topic) %>%
  summarise(strength = sum(abs(weight)))

# aggregate to concept level
agg_mat <- matrix(NA, nrow = 22, ncol = 22)
rownames(agg_mat) <- topic_refdf$topic
colnames(agg_mat) <- topic_refdf$topic

graph_rownames <- rownames(pundits_net_withparty$sample$graph)
graph_colnames <- rownames(pundits_net_withparty$sample$graph)

for(i in rownames(agg_mat)){
  for(j in colnames(agg_mat)){
    agg_mat[i,j] <-  sum(abs(pundits_net_withparty$sample$graph[grepl(i, graph_rownames), grepl(j, graph_colnames)]))
  }
}

# put aggregated/weighted adjacency matrix into graph
agg_abs <- igraph::graph_from_adjacency_matrix(agg_mat, 
                                               mode = "undirected", 
                                               weighted = TRUE,
                                               diag = FALSE)


# get diameter (longest shortest path)
d <- get_diameter(agg_abs, directed = FALSE, unconnected = FALSE)

# reweight by absolute value (magnitude of relationship, regardless of direction)
g_abs <- g
E(g_abs)$weight <- abs(E(g_abs)$weight) 

net_agg <- intergraph::asNetwork(agg_abs)

# aggregte strength centrality
agg_strength <- 
  data.frame(topic = names(igraph::strength(agg_abs)),
           strength = igraph::strength(agg_abs)) %>%
  left_join(topic_refdf, by = "topic") %>%
  dplyr::select(formal_names, strength) %>%
  arrange(desc(strength)) %>%
  ggplot(aes(x = fct_rev(fct_inorder(formal_names)),
             y = strength))+
  geom_col()+
  coord_flip()+
  labs(x = "",
       y = "Aggregate Strength Centrality",
       title = "Concept Centrality")+
  theme_jg()
ggsave(agg_strength, file = "../figures/aggregated_strength_centrality.png", width = 10, height = 6)
agg_strength$data %>%
         write.csv(file = "../figures/aggregated_strength_centrality_data.csv")

# plot

set.seed(11111)
gga <- ggnetwork(net_agg) %>%
  left_join(topic_refdf, by = c("vertex.names" = "topic"))

plota <- 
  gga %>%
  ggplot(aes(x = x, y = y, xend = xend, 
             yend = yend))+
  geom_edges(aes(alpha = abs(weight),
                 linewidth = weight))+
  geom_nodelabel(aes(label = formal_names), size = 3)+
  scale_alpha_continuous(name = "Tie Strength")+
  scale_color_discrete(name = "Issue Area")+
  guides(linewidth = "none",
         alpha = guide_legend(override.aes = list(size = 2)))+
  labs(title= "Aggregate Network")+
  theme_jg()+
  theme(axis.ticks = element_blank(),
        axis.title = element_blank(),
        axis.text = element_blank(),
        plot.title = element_text(size = 20),
        plot.subtitle = element_text(size = 14),
        legend.title = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.position = "bottom",
        legend.margin=margin(-35, 0, 0, 0))
ggsave(plota, file = paste0("../figures/agg_net_plot.png"), width = 12, height = 10)
plota$data %>%
  write.csv(file = "../figures/agg_net_plot_data.csv")

library(patchwork)
cap1 <- "Tie strength (Panel A) reflects sum of absolute penalized inverse covariance between first ten dimensions of each concept"
cap2 <- "Concept centrality (Panel B) reflects sum of between-concept tie strength"


# make figure 1
pundits_fig_1 <- plota + agg_strength +
  plot_layout(widths = c(2, 1))+
  plot_annotation(tag_levels = "A",
                 # title = "Figure 1. Aggregate Discursive Network",
                  caption = paste0(cap1, "\n", cap2),
                  theme = theme_jg()+
                    theme(plot.caption = element_text(hjust = 0)))

ggsave(pundits_fig_1, file = "../figures/figure_1.png", width = 14, height = 6)
ragg::agg_tiff("../figures/figure_1.tiff", 
    width=14, height=8, units = "in", 
    res = 300)
print(pundits_fig_1)
dev.off()